package com.lht.admin.controller;

import com.lht.admin.entity.SysUserEntity;
import com.lht.admin.service.CommonService;
import com.lht.admin.vo.OptionsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *
 * @author cs12110 create at 2020-01-01 19:08
 * <p>
 * @since 1.0.0
 */
@RestController
@RequestMapping("/common/")
@Slf4j
public class CommonController extends AbstractController {

    @Resource
    private CommonService commonService;


    /**
     * 获取options的值
     *
     * @return OptionsVO
     */
    @RequestMapping("/getOptionValues")
    public OptionsVO getOptionValues() {
        SysUserEntity user = getUser();
        return commonService.getOptionValues(user);
    }


    @RequestMapping("/getProvince")
    public List<OptionsVO.OptionVO> getProvince() {
        return commonService.getProvince();
    }


    @RequestMapping("/getCity/{provinceCode}")
    public List<OptionsVO.OptionVO> getCity(@PathVariable("provinceCode") String provinceCode) {
        return commonService.getCity(provinceCode);
    }

    @RequestMapping("/getArea/{cityCode}")
    public List<OptionsVO.OptionVO> getArea(@PathVariable("cityCode") String cityCode) {
        return commonService.getArea(cityCode);
    }


    /**
     * 下载文件
     *
     * @param response {@link HttpServletResponse}
     * @param fileName 文件名称
     */
    @RequestMapping("/download/{fileName}")
    public void download(HttpServletResponse response, @PathVariable("fileName") String fileName) {
        log.info("Function[download] file:{}", fileName);
        commonService.download(response, fileName);
    }


    /**
     * 上传文件
     *
     * @param file 文件对象
     * @return Object
     */
    @RequestMapping("/upload")
    public Object upload(MultipartFile file) {
        return commonService.uploadFile(file);
    }
}
